package edu.fim.presentation.view;

import java.awt.Component;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.AbstractButton;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableModel;

import edu.fim.presentation.controller.ConsumerController;
import edu.fim.presentation.controller.MealController;
import edu.fim.presentation.model.bean.Meal;
import edu.fim.presentation.model.bean.Portion;
import edu.fim.presentation.view.model.ScheduleTableModel;

public class ScheduleForm extends AbstractForm {

	private static final long serialVersionUID = -431458557028788286L;
	
	private ConsumerController consumerController;
	private JTable mealTable;
	private MealController mealController;
	
	public ScheduleForm(String title, ConsumerController consumerController) {
		super(title);
		this.consumerController = consumerController;
		mealController = new MealController();
		initialize();
		update();
	}
	
	public void update() {
		mealTable.setModel(createMealTableModel());
		pack();
	}

	protected void initializeDynamicComponents() {
		mealTable = new JTable();
	}

	protected void buildLayout() {
		getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
		JScrollPane scrollPane = new JScrollPane(mealTable);
		getContentPane().add(scrollPane);
		getContentPane().add(createButtonContainer());
	}
	
	private TableModel createMealTableModel() {
		List<Meal> meals = consumerController.getCurrentConsumer().getMeals();
		List<Portion> portions = new ArrayList<>();
		for (Meal meal : meals) {
			portions.addAll(meal.getPortions());
		}
		return new ScheduleTableModel(portions);
	}

	private Container createButtonContainer() {
		Container container = new Container();
		BoxLayout layout = new BoxLayout(container, BoxLayout.X_AXIS);
		container.setLayout(layout);
		container.add(createAddButton());
		container.add(createConfirmButton());
		container.add(createCancelButton());
		return container;
	}
	
	private Component createAddButton() {
		AbstractButton button = new JButton("Add Meal...");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				addButtonActionPerformed();
			}
		});
		return button;
	}
	
	private Component createConfirmButton() {
		AbstractButton button = new JButton("Save");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				confirmButtonActionPerformed();
			}
		});
		return button;
	}
	
	private Component createCancelButton() {
		AbstractButton button = new JButton("Cancel");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				cancelButtonActionPerformed();
			}
		});
		return button;
	}
	
	private void addButtonActionPerformed() {
		Form addMealForm = new AddMealForm("Add Meal", consumerController, mealController);
		addMealForm.setVisible(true);
	}
	
	private void confirmButtonActionPerformed() {
		
	}
	
	private void cancelButtonActionPerformed() {
		this.setVisible(false);
	}
}